
package cn.dakaqi.utils.sms.sms189;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AccesssToken
{
	static Logger log = LoggerFactory.getLogger(AccesssToken.class);
	private static final long serialVersionUID = 1L;
	private static final String accessTokenBasePath = "https://oauth.api.189.cn/emp/oauth2/v3/access_token";
	public static String code()
	{
		return null;
	}
	public static String getAccessToken() throws Exception
	{
//		Map<String,String> paras = new HashMap<String,String>();
//		paras.put("grant_type","client_credentials");
//		paras.put("app_id",SMS.app_id);
//		paras.put("app_secret",SMS.app_secret);
//					
//		String result = UnicodeHelper.decodeUnicode(FormSubmit.sendRequest(accessTokenBasePath,paras, FormSubmit.METHORD_POST));
//		return pareAccessToken(result);
		
		String postUrl = "https://oauth.api.189.cn/emp/oauth2/v3/access_token";
		String postEntity = "app_id=" + SMS.app_id
        + "&app_secret=" + SMS.app_secret
        + "&grant_type=client_credentials";
		
		return pareAccessToken(HttpInvoker.httpPost(postUrl,null,postEntity));
		
	}
	public static String getRefreshToken(String at) throws Exception
	{
//		Map<String,String> paras = new HashMap<String,String>();
//		paras.put("grant_type","refresh_token");
//		paras.put("refresh_token",getAccessToken());
//		paras.put("app_id",SMS.app_id);
//		paras.put("app_secret",SMS.app_secret);
//					
//		String result = UnicodeHelper.decodeUnicode(FormSubmit.sendRequest(refreshAccessTokenBasePath,paras, FormSubmit.METHORD_POST));
//		log.info(result);
//		return pareRefreshAccessToken(result);
		
//		String postUrl = refreshAccessTokenBasePath + "?app_id=" + SMS.app_id + "&app_secret=" + SMS.app_secret + "&grant_type=refresh_token&refresh_token=ed2a152b687a5c0ad2207bdbb8c261611399182005042";
		
		String postUrl = "https://oauth.api.189.cn/emp/oauth2/v3/access_token";
		String postEntity = "app_id=" + SMS.app_id
        + "&app_secret=" + SMS.app_secret
        + "&grant_type=refresh_token&refresh_token=" + at;
		
		String result =  HttpInvoker.httpPost(postUrl,null,postEntity);
		return pareAccessToken(result);

	}
	
	private static String pareAccessToken(String result)throws Exception
	{
		String access_token = "";
		//log.info(result);
		//{"res_code":"0","res_message":"Success","access_token":"ed2a152b687a5c0ad2207bdbb8c261611399182005042","expires_in":2591689}
		if(result != null && !result.trim().equals(""))
		{
			JSONObject jsonObject = JSON.parseObject(result);
			if(jsonObject != null)
			{
				int res_code = jsonObject.getInteger("res_code");
				if(res_code == 0)
				{
					String res_message = jsonObject.getString("res_message");
				    access_token = jsonObject.getString("access_token");
				    String expires_in = jsonObject.getString("expires_in");
				}
			}
			else
			{
				throw new Exception("数据为空");
			}
		}
		else
		{
			throw new Exception("格式不正确");
		}
		return access_token;
	}
	public static void main(String[] args)
	{
		try
		{
			//System.out.println(getAccessToken());
			System.out.println(getRefreshToken("ed2a152b687a5c0ad2207bdbb8c261611399182005042"));
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();log.error(e.getMessage());

		}
	}
}
